<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
      <div
        key="1"
        style="
          margin-right: auto;
          font-weight: normal;
          display: flex;
          align-items: center;
          color: rgb(96, 114, 166);
          font-size: 15px;
        "
      >
        第 {{ currentPage }} / {{ lastPage }} 页
      </div>
    </el-pagination>
  </div>
</template>

<script>
import { scrollTo } from "@/utils/scroll-to";

export default {
  name: "Pagination",
  props: {
    total: {
      required: true,
      type: Number,
    },
    page: {
      type: Number,
      default: 1,
    },
    limit: {
      type: Number,
      default: 20,
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50];
      },
    },
    // 移动端页码按钮的数量端默认值5
    pagerCount: {
      type: Number,
      default: document.body.clientWidth < 992 ? 5 : 7,
    },
    layout: {
      type: String,
      default: "total,slot, prev, pager, next",
    },
    background: {
      type: Boolean,
      default: true,
    },
    autoScroll: {
      type: Boolean,
      default: true,
    },
    hidden: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {};
  },
  computed: {
    currentPage: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit("update:page", val);
      },
    },
    lastPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    pageSize: {
      get() {
        return this.limit;
      },
      set(val) {
        this.$emit("update:limit", val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.DOMOperations();
    });
  },
  methods: {
    DOMOperations() {
      // 获取分页组件的元素
      const btnPrev = document.querySelector(".btn-prev");
      const elPager = document.querySelector(".el-pager");
      const btnNext = document.querySelector(".btn-next");

      // 检查这些元素是否存在
      if (btnPrev && elPager && btnNext) {
        console.log("1111111");
        // 创建一个新的父级 div
        const wrapperDiv = document.createElement("div");
        wrapperDiv.style.border = "1px solid #ccc"; // 添加边框样式
        wrapperDiv.style.padding = "5px"; // 添加内边距
        wrapperDiv.style.display = "flex"; // 使其成为 flex 布局

        // 插入新 div，把它放到 btnPrev 的前面
        btnPrev.parentNode.insertBefore(wrapperDiv, btnPrev);

        // 将目标元素移动到新创建的父级 div 中
        wrapperDiv.appendChild(btnPrev);
        wrapperDiv.appendChild(elPager);
        wrapperDiv.appendChild(btnNext);
      }
    },
    handleSizeChange(val) {
      if (this.currentPage * val > this.total) {
        this.currentPage = 1;
      }
      this.$emit("pagination", { page: this.currentPage, limit: val });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    },
    handleCurrentChange(val) {
      this.$emit("pagination", { page: val, limit: this.pageSize });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    },
  },
};
</script>

<style scoped>
.pagination-container {
  /* background: #fff; */
  padding: 0px 16px;
  /* float: right; */
}

.pagination-container.hidden {
  display: none;
}

::v-deep .el-pagination {
  display: flex;
  width: 100% !important;
  align-items: center;
  margin-top: -10px;
}
</style>
